package com.ruoyi.blindBoxManage.service.impl;

import com.ruoyi.blindBoxManage.domain.BlindBoxPublicProduct;
import com.ruoyi.blindBoxManage.mapper.BlindBoxProductMapper;
import com.ruoyi.blindBoxManage.mapper.BlindBoxPublicProductMapper;
import com.ruoyi.blindBoxManage.mapper.BlindProductMapper;
import com.ruoyi.blindBoxManage.service.IBlindBoxPublicProductService;
import com.ruoyi.common.utils.DateUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 盲盒公共商品Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-05
 */
@Service
public class BlindBoxPublicProductServiceImpl implements IBlindBoxPublicProductService {
    @Autowired
    private BlindBoxPublicProductMapper blindBoxPublicProductMapper;
    @Resource
    private BlindBoxProductMapper blindBoxProductMapper;
    @Resource
    private BlindProductMapper blindProductMapper;

    /**
     * 查询盲盒公共商品
     *
     * @param id 盲盒公共商品主键
     * @return 盲盒公共商品
     */
    @Override
    public BlindBoxPublicProduct selectBlindBoxPublicProductById(Long id) {
        return blindBoxPublicProductMapper.selectBlindBoxPublicProductById(id);
    }

    /**
     * 查询盲盒公共商品列表
     *
     * @param blindBoxPublicProduct 盲盒公共商品
     * @return 盲盒公共商品
     */
    @Override
    public List<BlindBoxPublicProduct> selectBlindBoxPublicProductList(BlindBoxPublicProduct blindBoxPublicProduct) {
        return blindBoxPublicProductMapper.selectBlindBoxPublicProductList(blindBoxPublicProduct);
    }

    /**
     * 新增盲盒公共商品
     *
     * @param blindBoxPublicProduct 盲盒公共商品
     * @return 结果
     */
    @Override
    public int insertBlindBoxPublicProduct(BlindBoxPublicProduct blindBoxPublicProduct) {
        blindBoxPublicProduct.setRemainStock(blindBoxPublicProduct.getTotalStock());
        blindBoxPublicProduct.setCreateTime(DateUtils.getNowDate());
        return blindBoxPublicProductMapper.insertBlindBoxPublicProduct(blindBoxPublicProduct);
    }

    /**
     * 修改盲盒公共商品
     *
     * @param blindBoxPublicProduct 盲盒公共商品
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateBlindBoxPublicProduct(BlindBoxPublicProduct blindBoxPublicProduct) {
        //修改盲盒所有商品
        blindProductMapper.updateBanch(blindBoxPublicProduct);
        //修改箱子所有商品
        blindBoxProductMapper.updateBanch(blindBoxPublicProduct);
        blindBoxPublicProductMapper.updatePackageBanch(blindBoxPublicProduct);

        blindBoxPublicProduct.setUpdateTime(DateUtils.getNowDate());
        return blindBoxPublicProductMapper.updateBlindBoxPublicProduct(blindBoxPublicProduct);
    }

    /**
     * 批量删除盲盒公共商品
     *
     * @param ids 需要删除的盲盒公共商品主键
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int deleteBlindBoxPublicProductByIds(Long[] ids) {
        for (Long id : ids) {
            //查询盲盒商品是否绑定公共商品
            Integer count = blindBoxProductMapper.selectCountByPublicProductId(id);
            if (count > 0) {
                throw new RuntimeException("id为:" + id + " 有商品绑定");
            }
        }
        return blindBoxPublicProductMapper.deleteBlindBoxPublicProductByIds(ids);
    }

    /**
     * 删除盲盒公共商品信息
     *
     * @param id 盲盒公共商品主键
     * @return 结果
     */
    @Override
    public int deleteBlindBoxPublicProductById(Long id) {
        return blindBoxPublicProductMapper.deleteBlindBoxPublicProductById(id);
    }
}
